home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / Mail / vars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-08  |  3.3 KB  |  174 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)vars.c    5.5 (Berkeley) 6/29/88";
  20. #endif /* not lint */
  21.  
  22. #include "rcv.h"
  23.  
  24. /*
  25.  * Mail -- a mail program
  26.  *
  27.  * Variable handling stuff.
  28.  */
  29.  
  30. /*
  31.  * Assign a value to a variable.
  32.  */
  33.  
  34. assign(name, value)
  35.     char name[], value[];
  36. {
  37.     register struct var *vp;
  38.     register int h;
  39.  
  40.     h = hash(name);
  41.     vp = lookup(name);
  42.     if (vp == NOVAR) {
  43.         vp = (struct var *) calloc(sizeof *vp, 1);
  44.         vp->v_name = vcopy(name);
  45.         vp->v_link = variables[h];
  46.         variables[h] = vp;
  47.     }
  48.     else
  49.         vfree(vp->v_value);
  50.     vp->v_value = vcopy(value);
  51. }
  52.  
  53. /*
  54.  * Free up a variable string.  We do not bother to allocate
  55.  * strings whose value is "" since they are expected to be frequent.
  56.  * Thus, we cannot free same!
  57.  */
  58.  
  59. vfree(cp)
  60.     char *cp;
  61. {
  62.     if (*cp)
  63.         free(cp);
  64. }
  65.  
  66. /*
  67.  * Copy a variable value into permanent (ie, not collected after each
  68.  * command) space.  Do not bother to alloc space for ""
  69.  */
  70.  
  71. char *
  72. vcopy(str)
  73.     char str[];
  74. {
  75.     char *new;
  76.     unsigned len;
  77.  
  78.     if (*str == '\0')
  79.         return "";
  80.     len = strlen(str) + 1;
  81.     if ((new = malloc(len)) == NULL)
  82.         panic("Out of memory");
  83.     bcopy(str, new, (int) len);
  84.     return new;
  85. }
  86.  
  87. /*
  88.  * Get the value of a variable and return it.
  89.  * Look in the environment if its not available locally.
  90.  */
  91.  
  92. char *
  93. value(name)
  94.     char name[];
  95. {
  96.     register struct var *vp;
  97.  
  98.     if ((vp = lookup(name)) == NOVAR)
  99.         return(getenv(name));
  100.     return(vp->v_value);
  101. }
  102.  
  103. /*
  104.  * Locate a variable and return its variable
  105.  * node.
  106.  */
  107.  
  108. struct var *
  109. lookup(name)
  110.     register char name[];
  111. {
  112.     register struct var *vp;
  113.  
  114.     for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link)
  115.         if (*vp->v_name == *name && equal(vp->v_name, name))
  116.             return(vp);
  117.     return(NOVAR);
  118. }
  119.  
  120. /*
  121.  * Locate a group name and return it.
  122.  */
  123.  
  124. struct grouphead *
  125. findgroup(name)
  126.     register char name[];
  127. {
  128.     register struct grouphead *gh;
  129.  
  130.     for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link)
  131.         if (*gh->g_name == *name && equal(gh->g_name, name))
  132.             return(gh);
  133.     return(NOGRP);
  134. }
  135.  
  136. /*
  137.  * Print a group out on stdout
  138.  */
  139.  
  140. printgroup(name)
  141.     char name[];
  142. {
  143.     register struct grouphead *gh;
  144.     register struct group *gp;
  145.  
  146.     if ((gh = findgroup(name)) == NOGRP) {
  147.         printf("\"%s\": not a group\n", name);
  148.         return;
  149.     }
  150.     printf("%s\t", gh->g_name);
  151.     for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
  152.         printf(" %s", gp->ge_name);
  153.     putchar('\n');
  154. }
  155.  
  156. /*
  157.  * Hash the passed string and return an index into
  158.  * the variable or group hash table.
  159.  */
  160.  
  161. hash(name)
  162.     register char *name;
  163. {
  164.     register h = 0;
  165.  
  166.     while (*name) {
  167.         h <<= 2;
  168.         h += *name++;
  169.     }
  170.     if (h < 0 && (h = -h) < 0)
  171.         h = 0;
  172.     return (h % HSHSIZE);
  173. }
  174.